import 'dart:async';

import 'package:kmxy_base/base/base_package.dart';

class PermissionDialog extends StatefulWidget {
  PermissionDialog({Key? key}) : super(key: key);

  @override
  _PermissionDialogState createState() => _PermissionDialogState();
}

class _PermissionDialogState extends State<PermissionDialog> {
  StreamSubscription? _subscription;

  ///  0-未申请授权，1-同意授权，2-拒绝授权
  int phoneState = 0;
  int storageState = 0;
  int cameraState = 0;
  int micropState = 0;
  int locationState = 0;

  @override
  void initState() {
    initData();
    super.initState();
  }

  initData() async {
    phoneState =
        await PermissionUtil.getInstance.isRequest(context, Permission.phone);
    storageState =
        await PermissionUtil.getInstance.isRequest(context, Permission.storage);
    cameraState =
        await PermissionUtil.getInstance.isRequest(context, Permission.camera);
    micropState = await PermissionUtil.getInstance
        .isRequest(context, Permission.microphone);
    locationState = await PermissionUtil.getInstance
        .isRequest(context, Permission.location);
    setState(() {});
    _subscription = Event.eventBus.on<Event>().listen((event) async {
      if (PermissionUtil.getInstance.EVENT_PERMISSION == event.type) {
        phoneState = await PermissionUtil.getInstance
            .isRequest(context, Permission.phone);
        storageState = await PermissionUtil.getInstance
            .isRequest(context, Permission.storage);
        cameraState = await PermissionUtil.getInstance
            .isRequest(context, Permission.camera);
        micropState = await PermissionUtil.getInstance
            .isRequest(context, Permission.microphone);
        locationState = await PermissionUtil.getInstance
            .isRequest(context, Permission.location);
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  setStatePermission(int state) {
    if (state == 0) return Icons.error;
    if (state == 1) return Icons.check_circle;
    if (state == 2) return Icons.not_interested;
    return Icons.error;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(false),
      child: Material(
        //创建透明层
        type: MaterialType.transparency, //透明类型
        child: Center(
          //保证控件居中效果
          child: SizedBox(
            width: MediaQuery.of(context).size.width - 30.w,
            child: Container(
              padding: EdgeInsets.all(20.w),
              decoration: BoxDecoration(
                color: Colors.white,
                // 圆角设置
                borderRadius: BorderRadius.circular(8),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    '为了保证应用正常使用\n请允许以下权限',
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 18.w),
                  ),
                  SizedBox(height: 20.w),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    child: Row(
                      children: [
                        Icon(Icons.call, size: 26.w),
                        SizedBox(width: 10.w),
                        Text(
                          '电话权限',
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 16.w),
                        ),
                        Expanded(child: Container()),
                        Icon(setStatePermission(phoneState), size: 26.w),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    child: Row(
                      children: [
                        Icon(Icons.storage, size: 26.w),
                        SizedBox(width: 10.w),
                        Text(
                          '存储权限',
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 16.w),
                        ),
                        Expanded(child: Container()),
                        Icon(setStatePermission(storageState), size: 26.w),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    child: Row(
                      children: [
                        Icon(Icons.camera_alt, size: 26.w),
                        SizedBox(width: 10.w),
                        Text(
                          '拍照权限',
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 16.w),
                        ),
                        Expanded(child: Container()),
                        Icon(setStatePermission(cameraState), size: 26.w),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    child: Row(
                      children: [
                        Icon(Icons.mic, size: 26.w),
                        SizedBox(width: 10.w),
                        Text(
                          '录音权限',
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 16.w),
                        ),
                        Expanded(child: Container()),
                        Icon(setStatePermission(micropState), size: 26.w),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    child: Row(
                      children: [
                        Icon(Icons.location_on, size: 26.w),
                        SizedBox(width: 10.w),
                        Text(
                          '定位权限',
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 16.w),
                        ),
                        Expanded(child: Container()),
                        Icon(setStatePermission(locationState), size: 26.w),
                      ],
                    ),
                  ),
                  SizedBox(height: 20.w),
                  InkWell(
                    onTap: () async {
                      await PermissionUtil.getInstance
                          .request(context, Permission.phone, '电话权限');
                      await PermissionUtil.getInstance
                          .request(context, Permission.storage, '存储权限');
                      await PermissionUtil.getInstance
                          .request(context, Permission.camera, '拍照权限');
                      await PermissionUtil.getInstance
                          .request(context, Permission.microphone, '录音权限');
                      await PermissionUtil.getInstance
                          .request(context, Permission.location, '定位权限');
                      Navigator.pop(context);
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 10.w),
                      child: Text(
                        '一键开启',
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 18.w),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
